package com.emmmya.ocs.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emmmya.ocs.common.utils.HResourcesUtil;
import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.modules.system.dao.mapper.UserMapper;
import com.emmmya.ocs.modules.system.entity.Menu;
import com.emmmya.ocs.modules.system.entity.User;
import com.emmmya.ocs.modules.system.service.MenuService;
import com.emmmya.ocs.modules.system.service.UserService;
import com.emmmya.ocs.modules.system.utils.UserUtils;
import org.apache.shiro.session.mgt.SessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @description:
 * @author: 咕噜肉IT小店-黑医
 * @createDate: 2020-05-21
 * @version: 1.0
 */
@RequestMapping("/test")
@RestController
public class TestController {
    @Autowired
    UserUtils userUtils;
    @Autowired
    UserService userService;
    @Autowired
    MenuService menuService;

    @Autowired
    UserMapper userMapper;

    /**
     * 获取配置文件属性的类
     */
    @Autowired
    private Environment env;

    @Autowired()
    private SessionManager sessionManager;

    @RequestMapping(value="resource")
    public Result<Object> getResources() throws IOException {
//        ClassPathResource classPathResource = new ClassPathResource("static"+File.separator+"assets"+ File.separator+"img"+File.separator+"1.jpg");
//        InputStream inputStream = classPathResource.getInputStream();
//        FileOutputStream out = new FileOutputStream("e://copy.jpg");
//        byte[] bytes = new byte[1024];
//        int i = 0;
//        while((i=inputStream.read(bytes))!=-1){
//            out.write(bytes,0,i);
//        }

        File templates = HResourcesUtil.getResources("templates");
        boolean directory = templates.isDirectory();
            if(directory){
                String[] fileNames = templates.list();
                System.out.println(Arrays.toString(fileNames));
        }
        return ResultUtil.success(templates.getPath());
    }

    @RequestMapping("/ex")
    public String exceptionTest(){
        int i = 1/0;
        return null;
    }

    @RequestMapping(value="/getCurrentUserToken",method = {RequestMethod.POST,RequestMethod.GET})
    public String getCurrentUserToken(){
        return userUtils.getCurrentUserToken();
    }

    @RequestMapping(value="/getCurrentUser",method = {RequestMethod.POST,RequestMethod.GET})
    public Result<Object> getCurrentUser(){
        User currentUser = userUtils.getCurrentUser();
        return ResultUtil.data(currentUser);
    }


    @RequestMapping(value="getUserPermission",method= {RequestMethod.POST,RequestMethod.GET})
    public Result<Object> getUserPermission(){
            return null;
    }




    @RequestMapping(value="getUserById",method= {RequestMethod.POST,RequestMethod.GET})
    public Result<Object> getUserById(){
        User user = userService.get(1L);
        return ResultUtil.data(user);
    }


    @RequestMapping(value="getMenuById",method= {RequestMethod.POST,RequestMethod.GET})
    public Result<Object> getMenuById(){
        Menu menu = menuService.get(1L);
        return ResultUtil.data(menu);
    }

    @RequestMapping("/RSA")
    public Result<Object> rsa(){
        try {
            Resource resource = new ClassPathResource("RSA/privateKey.properties");
            File file = resource.getFile();
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            String privateKey = properties.getProperty("privateKey");
            if(file.exists()){
                return ResultUtil.data(privateKey);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/cancelAll", method = RequestMethod.POST)
    public Result<Object> cancel(@RequestParam HashMap<String,String> idAndInsId,
                                 @RequestParam(required = false) String reason){
        if(idAndInsId== null || idAndInsId.size() == 0){
            return ResultUtil.error("传入参数有误！请检查后再提交。");
        }
        Set<String> ids = idAndInsId.keySet();
        for(String id:ids){
            String insId = idAndInsId.get(id);
            System.out.println("id:"+id +",insId:"+insId);
        }
        return ResultUtil.success("成功");
    }


    @RequestMapping("/testqiantao")
    @ResponseBody
    public Result<Object> testqiantao(){
        List<User> plainUsers3 = userMapper.selectList(new QueryWrapper<User>()
                .nested(i -> i.eq("role_id", 2L).or().eq("role_id", 3L))
                .and(i -> i.ge("age", 20)
                ));
        return ResultUtil.data(plainUsers3);
    }

    @RequestMapping("/testzicha")
    @ResponseBody
    public Result<Object> testzicha(){
        List<User> plainUsers2 = userMapper.selectList(new QueryWrapper<User>()
                .inSql("role_id", "select id from role where id = 2"));
        return ResultUtil.data(plainUsers2);
    }

    @RequestMapping("/testIPage")
    @ResponseBody
    public Result<Object> testIPage(@RequestParam("name") String name){
        IPage<User> userIPage1 = new Page<>(0,10);
        IPage<User> userIPage = userMapper.selectByName(userIPage1,name);
        return ResultUtil.data(userIPage);
    }

    @RequestMapping("getProperties")
    @ResponseBody
    public Result<Object> getProperties(String name){
        String properties = env.getProperty(name);
        return ResultUtil.data(name+"::"+properties);
    }

}
